// @flow
import React from 'react'
import { Icon } from 'antd'
import { withRouter } from 'react-router'
import styles from './Sidebar.css'

import type { Location } from 'react-router'

const WIDTH = '227px'

type Props = {
  id: string,
  type: string,
  title: string,
  style: Object,
  sidebar: Object,
  children: Element,
  location: Location,
  toggleSidebar: () => void
}

const sidebarClasses = (show: boolean, type: string): string => {
  let classes
  switch (type) {
    case 'float':
      classes = styles['sidebar--float'] + ' '
      break
    case 'pin':
      classes = styles['sidebar--pin'] + ' '
      break
    case 'dock':
      classes = styles['sidebar--dock'] + ' '
      break
    default:
      classes = ''
  }
  return classes + (show ? styles['sidebar--show'] : styles['sidebar--hidden'])
}
const { search } = location
const paramsString = search.substring(1)
const searchParams = new URLSearchParams(paramsString)
const Sidebar = ({ location, sidebar, children, id, toggleSidebar, style: customStyles, type, title }: Props) => (
  <div className={sidebarClasses(sidebar[id], type)}
    style={(() => {
      if (id === 'deviceFile') {
        return customStyles
      } else {
        return searchParams.get('hide-sidebar') === 'true' ? Object.assign({}, customStyles, { display: 'none' })
          : customStyles
      }
    })()}
    ref={(elem) => {
      let current
      function draw (timestamp) {
        const clone = elem.querySelector('.clone')
        const title = elem.querySelector('.origin')
        if (type === 'pin' && !sidebar[id]) {
          title.style.width = clone.clientWidth + 'px'
          elem.style.width = clone.clientWidth + 'px'
        } else {
          elem.style.width = customStyles.width || WIDTH
          title.style.width = customStyles.width || WIDTH
        }
        if (!current || current !== clone.clientWidth) {
          current = clone.clientWidth
          window.requestAnimationFrame(draw)
        } else {

        }
      }

      if (elem && elem.style && type === 'pin') {
        window.requestAnimationFrame(draw)
      }
    }}
  >
    { type === 'pin' &&
      [
        <header key='clone' className={styles['title'] + ' clone'} style={{
          minWidth: '80px',
          display: 'inline-block',
          position: 'fixed',
          top: '-1000px',
          transition: 'none'
        }}>
          <span style={{ display: 'inline-block', marginRight: '30px' }}>{title}</span>
          <Icon type='down' />
        </header>,
        <header key='origin' className={styles['title'] + ' origin'}
          style={{ minWidth: '80px', display: 'inline-block', cursor:'pointer' }}
          onClick={() => toggleSidebar(id)}>
          <span style={{ display: 'inline-block', marginRight: '30px' }}>{title}</span>
          <Icon type={sidebar[id] ? 'down' : 'right'} />
        </header>
      ]
    }
    <div className={styles['content']}>
      {children}
    </div>
    <div className={styles['gear']} onClick={() => toggleSidebar(id)}>
      <svg version='1.1'
        baseProfile='full'
        width='10' height='10'
        viewBox='0 0 10 10'
        xmlns='http://www.w3.org/2000/svg'>
        <path d='M2 5 L7 0 V 10 L2 5' />
      </svg>
    </div>
  </div>
)

Sidebar.defaultProps = {
  style: {},
  title: 'title'
}

export default withRouter(Sidebar)
